package com.tianquan.mvc;/**
 * @author starsea
 * @date 2024-09-10 下午5:39
 */

import com.tianquan.log.ThreadLocalContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.Ordered;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 *@BelongsProject: uploaddemo
 *@BelongsPackage: com.tianquan
 *@Author: tianquan
 *@CreateTime: 2024-09-10  17:39
 *@Description: 测试
 *@Version: 1.0
 */

@Component
public class HandlerInterceptorTest implements HandlerInterceptor, Ordered {
    @Override
    public boolean preHandle(@NonNull HttpServletRequest request,@NonNull HttpServletResponse response,@NonNull Object handler) throws Exception {

        String method = request.getMethod();
        String requestURI = request.getRequestURI();
        String ip = request.getRemoteAddr();
        ThreadLocalContext.setPath(method + requestURI);
        ThreadLocalContext.setIp(ip);
        ThreadLocalContext.setUserId(1206L);

        System.out.println("preHandle---------------执行controller方法前-----------------------------");
        return true;
    }

    @Override
    public void postHandle(@NonNull HttpServletRequest request,@NonNull HttpServletResponse response,@NonNull Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle---------------执行controller方法后-----------------------------");
    }

    @Override
    public void afterCompletion(@NonNull HttpServletRequest request,@NonNull HttpServletResponse response,@NonNull Object handler, Exception ex) throws Exception {
        System.out.println("preHandle---------------执行完成后-----------------------------");
        ThreadLocalContext.clear();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
